const testModel = require("../models/testsModel")
// 考试记录添加
exports.addtest_c = async (req, res) => {
    try {
        // 使用默认值来避免未定义错误
        const {
            name,
            type,
            class_id,
            date,
            notes,
            write_of,
            practe_of,
            meet_of,
            project_of
        } = req.body;

        /**
            class_id: 2
            date: "2025-01-31T11:08:52.165Z"
            meet_of: 30
            name: "python测试考01月"
            notes: "主要考察python的基本功"
            practe_of: 50
            project_of: 0
            type: 0
            write_of: 20
         */
        console.log("传来的参数：：", req.body)
        /*
        传来的参数：： 
        {
            name: 'python测试考01月',
            type: 0,
            class_id: 2,
            date: '2025-01-31T11:14:18.160Z',
            notes: '主要考察python的基本功',
            write_of: 20,
            practe_of: 50,
            meet_of: 30,
            project_of: 0
        }
        */

        // 数据验证
        // 更安全的验证方式
        if (
            name === undefined || name === null || name === '' ||
            type === undefined || type === null ||
            class_id === undefined || class_id === null ||
            date === undefined || date === null || date === ''
        ) {
            console.log('缺失的字段:', {
                name: name === undefined || name === null || name === '',
                type: type === undefined || type === null,
                class_id: class_id === undefined || class_id === null,
                date: date === undefined || date === null || date === ''
            });
            return res.status(400).json({ code: -1, msg: '缺少必要的参数' });
        }

        // 计算总分比例
        const all_of = Number(write_of) + Number(practe_of) + Number(meet_of) + Number(project_of);

        // 添加考试记录
        const results = await testModel.addtest(name, type, class_id, date, Number(write_of),
            Number(practe_of), Number(meet_of), Number(project_of), all_of, notes);

        console.log(results);
        res.json({ code: 0, msg: "考试记录添加成功！", results });
    } catch (error) {
        console.error(error);
        res.status(500).json({ code: -1, msg: "考试记录添加失败！", error: error.message });
    }
};
// 考试记录删除--软删除
exports.deltest_f_c = async (req, res) => {
    // try...catch的方式
    try {
        let { id } = req.query
        const results = await testModel.deltest_f(id)
        console.log(results);
        res.json({ code: 0, msg: "考试记录禁用成功！", results });
    } catch (error) {
        console.error(error);
        res.status(500).json({ code: -1, msg: "考试记录禁用失败！", error: error.message });
    }
}
exports.start = async (req, res) => {
    // try...catch的方式
    try {
        let { id } = req.query
        const results = await testModel.start_state(id)
        console.log(results);
        res.json({ code: 0, msg: "考试记录启用成功！", results });
    } catch (error) {
        console.error(error);
        res.status(500).json({ code: -1, msg: "考试记录启用失败！", error: error.message });
    }
}
// 考试记录删除--真删除
exports.deltest_t_c = (req, res) => {
    // then...catch的方式
    let { id } = req.query
    const p = testModel.deltest_t(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "考试记录删除成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "考试记录删除失败！", error })
    })
}
// 修改考试记录--get数据 && 查询数据
exports.edit_get_c = (req, res) => {
    let { id } = req.query
    const p = testModel.edit_get(id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "修改考试记录获取数据成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "修改考试记录获取数据失败！", error })
    })
}
// 修改考试记录--set数据
exports.edit_set_c = (req, res) => {
    let { id, name, type, class_id, date, total_score, write_of, practe_of, meet_of, project_of, all_of, notes } = req.body
    const p = testModel.edit_set(id, name, type, class_id, date, total_score, write_of, practe_of, meet_of, project_of, all_of,
        notes)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "修改考试记录设置数据成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "修改考试记录设置数据失败！", error })
    })
}
// 考试列表搜索+分页
exports.test_list_c = async (req, res) => {
    try {
        // 验证输入参数
        const { name, class_id, program_id, start_date, end_date, type, page, page_size } = req.query;
        if (!page || !page_size) {
            return res.json({ code: -1, msg: "分页参数不能为空", results: [] });
        }

        // 调用模型函数获取考试列表
        const results = await testModel.test_list(name, Number(class_id), Number(program_id), start_date, end_date, type, page, page_size);
        console.log(results);
        res.json({ code: 0, msg: "考试列表搜索数据请求成功！", results });
    } catch (error) {
        console.error(error);
        res.json({ code: -1, msg: "考试列表搜索数据请求失败！", error: { msg: error.message } });
    }
};
// 对应的学生的成绩录入
exports.score_enter_c = (req, res) => {
    let { id, student_id, score_write, score_meet, score_practe, score_project, comment } = req.body
    testModel.score_enter(id, student_id, score_write, score_meet, score_practe, score_project, comment, (error, result) => {
        if (error) {
            console.log("对应的学生的成绩录入错误：", error)
            res.json({ code: -1, msg: "成绩录入失败！", result })
        } else {
            res.json({ code: 0, msg: "成绩录入成功！", result })
        }
    })
}
// 统计分析--根据所传的考试id查询成绩表（exam_results）
exports.test_count = (req, res) => {
    let { id, class_id, page, page_size } = req.query
    const p = testModel.test_count(id, class_id, Number(page), Number(page_size))
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "统计分析数据请求成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "统计分析数据请求失败！", error })
    })
}
// 根据班级的id去找这个班级对应的所有学生
// 班级对应的学生信息请求
exports.exam_student = (req, res) => {
    let { class_id, exam_id, page, page_size } = req.query
    const p = testModel.exam_student(class_id, exam_id, Number(page), Number(page_size))
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "班级对应的学生信息请求成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "班级对应的学生信息请求失败！", error })
    })
}

// 统计某个考试总数
exports.total_count = (req, res) => {
    let { exam_id, class_id } = req.query
    const p = testModel.count_total_item(exam_id, class_id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "统计当前考试总数成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "统计当前考试总数失败！", error })
    })
}

// 统计某个考试的平均分
exports.count_average = (req, res) => {
    let { exam_id, class_id } = req.query
    const p = testModel.count_average(exam_id, class_id)
    p.then((results) => {
        console.log(results)
        res.json({ code: 0, msg: "统计当前考试平均数成功！", results })
    }).catch((error) => {
        console.log(error)
        res.json({ code: -1, msg: "统计当前考试平均数失败！", error })
    })
}